package com.github.shiqiyue.app.config.security.sapi;


import com.github.shiqiyue.app.modules.sys.entity.User;
import com.github.shiqiyue.app.modules.sys.enums.UserStatusEnum;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;

/***
 * 登录的用户信息
 * @author wenyao.wu
 */
public class SapiLoginUser implements Serializable, UserDetails {

    private static final long serialVersionUID = 1L;

    /***
     * 用户信息
     */
    private User user;

    /***
     * 权限
     */
    private List<String> permissions;

    private List<SimpleGrantedAuthority> authorities;

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public List<String> getPermissions() {
        return permissions;
    }

    public void setPermissions(List<String> permissions) {
        this.permissions = permissions;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return authorities;
    }

    public void setAuthorities(List<SimpleGrantedAuthority> authorities) {
        this.authorities = authorities;
    }

    @Override
    public String getPassword() {
        return user.getPassword();
    }

    @Override
    public String getUsername() {
        return user.getName();
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    /***
     * 用户可不可以登录
     * @return
     */
    @Override
    public boolean isEnabled() {
        return UserStatusEnum.ABLE.equals(user.getStatus());
    }

}
